package com.talent.controller;

import com.talent.entities.dto.Vacc;
import com.talent.service.VaccListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.Objects;

/**
 * @author 周意朗
 * Date: 2022/3/22
 * @since JDK 11
 */
@RestController
@EnableAutoConfiguration
@Slf4j
@CrossOrigin
public class VaccListController {
    @Value("${server.port}")
    private String serverPort;

    @Resource
    VaccListService service;

    @GetMapping("/test/{id}")
    public String test(@PathVariable("id") String id) {
        return serverPort + ":,id: " + id;
    }

    @PostMapping("/mainpage/{index}")
    public LinkedList<Vacc> getVaccList(@PathVariable("index") int index) {
        return service.getVaccList(index);
    }

    @PostMapping("/mainpage/getvacclistbycondition")
    public LinkedList<Vacc> getVaccListByCondition(@RequestBody MultiValueMap<String, Integer> map) {
        Integer age = (Objects.requireNonNull(map.getFirst("age")));
        Integer index = (Objects.requireNonNull(map.getFirst("index")));
        Integer type = (Objects.requireNonNull(map.getFirst("type")));
        Integer gender = (Objects.requireNonNull(map.getFirst("gender")));

        return service.getVaccListByCondition(age, type, gender, index);
    }

    @PostMapping("/mainpage/getvacclistbysearch")
    public LinkedList<Vacc> getVaccListBySearch(@RequestBody MultiValueMap<String, String> map) {
        int index = Integer.parseInt(Objects.requireNonNull(map.getFirst("index")));
        String input = (Objects.requireNonNull(map.getFirst("input")));
        return service.getVaccListBySearch(input, index);
    }


    @PostMapping("/mainpage/getvacclistbysearchadmin/")
    public LinkedList<Vacc> getVaccListByserachAdmin(@RequestBody MultiValueMap<String, String> map) {
        int index = Integer.parseInt(Objects.requireNonNull(map.getFirst("index")));
        String input = (Objects.requireNonNull(map.getFirst("input")));
        return service.getVaccListByserachAdmin(input, index);
    }

    @PostMapping("/mainpage/getvacclist/addVacc")
    public Boolean addVaccByAdmin(@RequestBody Vacc addedVacc, HttpServletRequest request) {
        return service.addVaccByAdmin(addedVacc);
    }

    @PostMapping("/mainpage/getvacclist/update")
    public Boolean updateVacc(@RequestBody Vacc vacc) {
        return service.updateVacc(vacc);
    }

    @PostMapping("/mainpage/getvacclistbyid")
    public Vacc searchById(@RequestBody String id) {
        return service.searchById(id);
    }
}
